package com.example.huixue.mapper;

import com.example.huixue.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {
    
    /**
     * 根据用户名查找用户
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users WHERE username = #{username}")
    User findByUsername(String username);
    
    /**
     * 根据邮箱查找用户
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users WHERE email = #{email}")
    User findByEmail(String email);
    
    /**
     * 根据用户ID查找用户
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users WHERE user_id = #{userId}")
    User findById(Integer userId);
    
    /**
     * 插入新用户
     */
    @Insert("INSERT INTO users (username, password, email, phone, register_date, user_role, status) " +
            "VALUES (#{username}, #{password}, #{email}, #{phone}, #{registerDate}, #{userRole}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insert(User user);
    
    /**
     * 更新用户信息
     */
    @Update("UPDATE users SET username = #{username}, email = #{email}, phone = #{phone}, " +
            "profile_picture = #{profilePicture}, status = #{status} WHERE user_id = #{userId}")
    int update(User user);
    
    /**
     * 更新用户密码
     */
    @Update("UPDATE users SET password = #{password} WHERE user_id = #{userId}")
    int updatePassword(@Param("userId") Integer userId, @Param("password") String password);
    
    /**
     * 更新最后登录时间
     */
    @Update("UPDATE users SET last_login = #{lastLogin} WHERE user_id = #{userId}")
    int updateLastLogin(@Param("userId") Integer userId, @Param("lastLogin") LocalDateTime lastLogin);
    
    /**
     * 更新用户角色
     */
    @Update("UPDATE users SET user_role = #{role} WHERE user_id = #{userId}")
    int updateUserRole(@Param("userId") Integer userId, @Param("role") String role);
    
    /**
     * 更新用户状态
     */
    @Update("UPDATE users SET status = #{status} WHERE user_id = #{userId}")
    int updateUserStatus(@Param("userId") Integer userId, @Param("status") String status);
    
    /**
     * 删除用户
     */
    @Delete("DELETE FROM users WHERE user_id = #{userId}")
    int deleteById(Integer userId);
    
    /**
     * 分页查询用户列表
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users ORDER BY register_date DESC LIMIT #{offset}, #{limit}")
    List<User> findUsersWithPagination(@Param("offset") int offset, @Param("limit") int limit);
    
    /**
     * 查询用户总数
     */
    @Select("SELECT COUNT(*) FROM users")
    long countUsers();
    
    /**
     * 根据角色查询用户
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users WHERE user_role = #{role}")
    List<User> findByRole(String role);
    
    /**
     * 搜索用户（根据用户名或邮箱模糊匹配）
     */
    @Select("SELECT user_id as userId, username, password, email, phone, register_date as registerDate, " +
            "last_login as lastLogin, user_role as userRole, profile_picture as profilePicture, status " +
            "FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR email LIKE CONCAT('%', #{keyword}, '%') " +
            "ORDER BY register_date DESC LIMIT #{offset}, #{limit}")
    List<User> searchUsers(@Param("keyword") String keyword, @Param("offset") int offset, @Param("limit") int limit);
    
    /**
     * 搜索用户总数
     */
    @Select("SELECT COUNT(*) FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR email LIKE CONCAT('%', #{keyword}, '%')")
    long countSearchUsers(@Param("keyword") String keyword);
    
    /**
     * 根据角色统计用户数量
     */
    @Select("SELECT COUNT(*) FROM users WHERE user_role = #{role}")
    long countUsersByRole(String role);
} 